package com.derbysoft.ota.application.translator.rq;

import com.derbysoft.modules.eip.translator.Translator;
import com.derbysoft.ota.interfaces.protocol._2009a.GuaranteeType;
import com.derbysoft.ota.interfaces.protocol._2009a.GuaranteeType.GuaranteeTypeEnum;
import com.derbysoft.ota.interfaces.protocol._2009a.GuaranteeType.GuaranteesAccepted;
import com.derbysoft.ota.interfaces.protocol._2009a.GuaranteeType.GuaranteesAccepted.GuaranteeAccepted;
import com.derbysoft.ota.interfaces.protocol._2009a.PaymentCardType;
import com.derbysoft.remote.dswitch.generic.constants.CardCode;
import com.derbysoft.remote.dswitch.generic.constants.CardType;
import com.derbysoft.remote.dswitch.generic.dto.GuaranteeDTO;
import com.derbysoft.remote.dswitch.generic.dto.PaymentCardDTO;
import com.google.inject.Inject;
import com.googlecode.functionalcollections.FunctionalIterables;

public class GuaranteeRQTranslator implements Translator<GuaranteeType, GuaranteeDTO> {

	private Translator<String, CardCode> cardCodeTranslator;
	private Translator<String, CardType> cardTypeTranslator;
	private Translator<GuaranteeTypeEnum, com.derbysoft.remote.dswitch.hotel.constants.GuaranteeType> guaranteeTypeTranslator;

	@Inject
	public GuaranteeRQTranslator(Translator<String, CardCode> cardCodeTranslator,
								 Translator<String, CardType> cardTypeTranslator,
								 Translator<GuaranteeTypeEnum, com.derbysoft.remote.dswitch.hotel.constants.GuaranteeType> guaranteeTypeTranslator) {
		this.cardCodeTranslator = cardCodeTranslator;
		this.cardTypeTranslator = cardTypeTranslator;
		this.guaranteeTypeTranslator = guaranteeTypeTranslator;
	}

	public GuaranteeDTO translate(GuaranteeType source) {
		GuaranteeDTO guaranteeDTO = new GuaranteeDTO();
		guaranteeDTO.setGuaranteeType(guaranteeTypeTranslator.translate(source.getGuaranteeType()));
		GuaranteesAccepted accepted = source.getGuaranteesAccepted();
		if (accepted != null) {
			guaranteeDTO.setPaymentCard(paymentCard(FunctionalIterables.make(accepted.getGuaranteeAccepted())
					.messagedIterator("Please provider us at least 1 GuaranteeAccepted.").next()));
		}
		return guaranteeDTO;
	}

	private PaymentCardDTO paymentCard(GuaranteeAccepted guaranteeAccepted) {
		PaymentCardType otaPaymentCard = guaranteeAccepted.getPaymentCard();
		return new PaymentCardDTO(
				cardTypeTranslator.translate(otaPaymentCard.getCardType()),
				cardCode(otaPaymentCard),
				otaPaymentCard.getExpireDate(),
				otaPaymentCard.getCardNumber(),
				otaPaymentCard.getCardHolderName()
		);
	}

	private CardCode cardCode(PaymentCardType otaPaymentCard) {
		return cardCodeTranslator.translate(otaPaymentCard.getCardCode());
	}
}
